<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ApplyController extends Controller
{
    /**
     * 显示商铺申请
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        return view('Shop.apply.index');
    }

    /**
     * 执行申请
     * @return \Illuminate\Http\Response
     */
    public function postCreate(Request $request)
    {
        //表单验证
       $this->validate($request, [
            'sname' => 'required|unique:users,name|regex:/^\w{6,18}$/',
            'password' => 'required|regex:/^\S{6,18}$/',
            'phone' => 'required|regex:/^1[345678]\d{9}$/',
        ],[
            'sname.required'=>'用户名必填',
            'sname.unique'=>'用户名已经存在',
            'sname.regex'=>'用户名格式不正确',
            'password.required'=>'密码必填',
            // 'password.regex'=>'密码格式不正确',
            'phone.required'=>'手机号必填',
            'phone.regex' => '手机号不正确'
        ]);
        $data = $request->except('_token','content','site');
        $res = $request->get('sname');
        $arr = $request->except('_token','sname','password','phone');
        $arr['nickname'] = $res;
       //开启事务
        DB::beginTransaction();
        $res  = DB::table('shops')->insert($data);
        $resa = DB::table('shops_details')->insert($arr);
        if($res && $resa) {
            DB::commit();
            return redirect('/apply/index')->with('info','申请已提交,3-5工作日回复您');
        }else{
            DB::rollBack();
            return back()->with('info','信息不够完善提交');
        }
    }

    
}
